﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BizzCore;

namespace BizzUI.Forms
{
    public partial class FilterForm : Form
    {
        private List<MessageEntity> filteredMsg;
        private DataTable attributes;
        private BindingSource bindingDataTable = new BindingSource();
        private DateTime dateFrom;
        private DateTime dateto;
        private string inboxTxt;

        public FilterForm()
        {
            InitializeComponent();
            attributes = new DataTable();
            dateFrom = new DateTime(1990, 1, 1);
            dateto = DateTime.Now.AddDays(1);
            inboxTxt = "%%";
            
            initDataGrid();
        }

        private void initDataGrid()
        {
            bindingDataTable.DataSource = attributes;
            dgFilter.AutoGenerateColumns = false;
            attributes.Columns.Add("Atributo");
            
            attributes.Columns.Add("Condición");
            attributes.Columns.Add("Valor");
            attributes.Columns.Add("Operador");
            
            dgFilter.DataSource = bindingDataTable;
            
            
            dgFilter.RowHeadersVisible = false;
        
           

           DataGridViewTextBoxColumn nameCol = new DataGridViewTextBoxColumn();
           nameCol.HeaderText = "Atributo";
           DataGridViewComboBoxColumn condCol = new DataGridViewComboBoxColumn();
           condCol.HeaderText = "Condición";
           condCol.Items.Add("=");
           condCol.Items.Add("<");
           condCol.Items.Add(">");
           condCol.Items.Add("<=");
           condCol.Items.Add(">=");
           condCol.Items.Add("<>");
           DataGridViewTextBoxColumn valueCol = new DataGridViewTextBoxColumn();
           valueCol.HeaderText = "Valor";
           

           DataGridViewComboBoxColumn condLogicCol = new DataGridViewComboBoxColumn();
           condLogicCol.HeaderText = "Operador";
           condLogicCol.Items.Add("AND");
           condLogicCol.Items.Add("OR");
           condLogicCol.Items.Add("   ");

           dgFilter.Columns.Insert(0,nameCol);
           dgFilter.Columns.Insert(1, condCol);
           dgFilter.Columns.Insert(2, valueCol);
           dgFilter.Columns.Insert(3, condLogicCol);

           dgFilter.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
           dgFilter.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
           dgFilter.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
           dgFilter.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            
        }

        


        private void buttonOK_Click(object sender, EventArgs e)
        {
            List<string[]> filter = new List<string[]>();
            foreach (DataGridViewRow row in dgFilter.Rows)
            {
                if (row.Cells[0].Value != null && row.Cells[1].Value != null && row.Cells[2].Value != null)
                {
                    if (row.Cells[3].Value == null) row.Cells[3].Value = "";
                    string[] field = new string[] { row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString(), row.Cells[2].Value.ToString(), row.Cells[3].Value.ToString() };
                    filter.Add(field);
                }
            }
            if (cbFrom.Checked) dateFrom = dtFrom.Value;
            if (cbTo.Checked) dateto = dtTo.Value;
            if (cbInbox.Checked) inboxTxt = textBoxInbox.Text;


            filteredMsg = BizzSystem.getInstance().getMessages(filter, dateFrom, dateto, inboxTxt);
            //attributes = (DataTable)dgFilter.DataSource;
            this.Hide();
        }

        public List<MessageEntity> getFilterResult()
        {
            return filteredMsg;
        }

        private void dgFilter_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dgFilter.IsCurrentCellDirty)
            {
                dgFilter.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }
    }
}

